package com.correction.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.correction.classes.Examen;
import com.correction.classes.Exercice;
import com.correction.classes.NoteExercice;
import com.correction.classes.Utilisateur;
import com.correction.classes.Utilisateur.Niveau;
import com.correction.utils.HibernateUtil;
import com.correction.utils.Tools;

public class ExamenDAO {

	public static Examen getExamen(int id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Examen examen = null;
		String hql = "FROM Examen e where (e.id='" + id + "')";
		examen = (Examen) session.createQuery(hql).uniqueResult();
		session.close();

		return examen;
	}

	public static void deleteExamen(Examen examen) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.delete(examen);
		session.getTransaction().commit();
		session.close();

	}

	public static List<Examen> getExamenByNiveau(Niveau niveau) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<Examen> examens = new ArrayList<Examen>();
		List<Utilisateur> profs = UtilisateurDAO.getProfesseurs();
		for (Utilisateur prof : profs) {

			List<Examen> examensList = prof.getExamens();
			for (Examen examen : examensList) {
				if (examen.getNiveau().equals(niveau))
					examens.add(examen);
			}
		}
		session.close();
		return examens;
	}

	public static List<Examen> getExamenByNiveau(Niveau niveau,
			List<Examen> examensList) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<Examen> examens = new ArrayList<Examen>();
		for (Examen examen : examensList) {
			if (examen.getNiveau().equals(niveau))
				examens.add(examen);
		}
		session.close();
		return examens;
	}

	public static List<Examen> getAll() {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<Examen> examens = null;
		try {
			transaction = session.beginTransaction();
			examens = session.createCriteria(Examen.class).list();
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return examens;
	}

	public static List<Examen> getFinichedExam(int idUser) {

		List<Examen> exams = new ArrayList<Examen>();
		Utilisateur utilisateur = UtilisateurDAO.getUtilisateur(idUser);

		List<Exercice> exosPassed = Tools.getExoPassed(utilisateur);

		List<Examen> examensByNiveau = getExamenByNiveau(utilisateur
				.getNiveau());


		for (Examen examen : examensByNiveau) {
			List<Exercice> exercices = examen.getExercices();

			boolean isOk = true;
			for (Exercice exercice : exercices) {
				if (!isPassed(exercice, exosPassed)) {
					isOk = false;
				}
			}
			if (isOk) {
				exams.add(examen);
			}
		}
		return exams;

	}

	private static boolean isPassed(Exercice exercice, List<Exercice> exosPassed) {
		for (Exercice exercicePassed : exosPassed) {
			if (exercice.getId() == exercicePassed.getId())
				return true;
		}
		return false;
	}

	public static List<Examen> getFinichedExam(int studentId,
			List<Examen> profExamens) {

		List<Examen> exams = new ArrayList<Examen>();

		Utilisateur utilisateur = UtilisateurDAO.getUtilisateur(studentId);
		List<Exercice> exosPassed = Tools.getExoPassed(utilisateur);
		for (Examen examen : profExamens) {
			List<Exercice> exercices = examen.getExercices();
			boolean isOk = true;
			for (Exercice exercice : exercices) {
				if (!exosPassed.contains(exercice)) {
					isOk = false;
				}
			}
			if (isOk)
				exams.add(examen);
		}

		return exams;
	}

}
